module.exports = function ($scope, $state, $resource, $modal, toaster) {

	//加载销售品分类下拉搜索
	$scope.loadSaleCategory = function (){
		$resource('/api/as/sc/dict/dictbytypelist', {}, {}).save({'type': 'sale_category'}, function(res){
			if(res.errcode === 0){
				$scope.sale_category = res.data;
				$scope.sale_category.splice(0,0,{'label': '全部', 'value': '', 'type': 'sale_category'});
			}else{
				toaster.error({title: '', body: '加载销售品列表失败'});
			}
		});
	}
	//加载渠道列表
	$scope.loadChannelList = function (){
		$resource('/api/ac/tc/officeBindService/getAllList', {}, {}).save({'tag': '2'}, function (res) {
			if (res.errcode === 0) {
				$scope.channel_list = res.data.channelList;
				$scope.channel_list.unshift({ name: '--全部渠道--', code: '' });
			} else {
				toaster.error({ title: "", body: res.errmsg })
			}
		});
	}

	//加载登录账号的协议价信息
	$scope.loadOfficeInfo = function (){
		$resource('/api/ac/tc/agreementPriceService/setOfficeInfo', {}, {}).save({}, function(res){
			if(res.errcode === 0){
				$scope.officeInfo = res.data;
			} else {
				toaster.error({title: '', body: '加载个人信息失败'});
			}
		});
	}

	//搜索我的采纳产品库
	$scope.search = function (pageNo){
		$scope.searchform.pageNo = $scope.currentPage;
		$scope.searchform.pageSize = $scope.itemsPerPage;
		$resource('/api/ac/tc/ticketSaleAuthService/findPlatformSaleList', {}, {}).save($scope.searchform, function(res){
			if(res.errcode === 0){
                $scope.sale_list = res.data.results;
                
				$scope.totalItems = res.data.totalRecord;
				$scope.loading = false;
				$scope.currentPage = pageNo;
			}else{
				toaster.error({title: '', body: res.errmsg});
			}
		});
	}
	
	init();
	//初始化查询产品列表
	function init(){
		$scope.currentPage = 1;			//当前页码
		$scope.itemsPerPage = 10;		//每页显示几条;
		$scope.moreSea = false;
		$scope.searchform = {};
		$scope.nothave = [
			{'name': '全部', 'value': null},
			{'name': '未采购', 'value': '0'},
			{'name': '已采购', 'value': '1'}
		];
		$scope.loading = true;
		$scope.search(1);
		$scope.loadSaleCategory();
		$scope.loadChannelList();
		$scope.loadOfficeInfo();
	}

    
	//设置公开范围
	$scope.change2 = function (info){
		var msg = info.downstream_open_company == '1'? '确认修改为公开销售吗?': '修改后分销商无法查看此产品?';
		if(confirm(msg)){
			var para = {
				'sale_code': info.code,
				'open_company': info.downstream_open_company
			};
			$resource('/api/ac/tc/ticketSaleAuthService/updateOpen', {}, {}).save(para, function (res) {
				if (res.errcode === 0 && res.data.msg === 'success') {
					toaster.success({title: '', body: '修改成功'});
				} else {
					toaster.error({title: '', body: '修改失败'});
					info.downstream_open_company = info.downstream_open_company == '1'?'0':'1';
				}
			});
		} else {
            info.downstream_open_company = info.downstream_open_company == '1'?'0':'1';
		}
	}

	//查询产品详情
	$scope.productInfo = function(info){
		$resource('/api/as/sc/dict/getSaleCategoryList', {}, {}).save({'type': 'sale_category'}, function(res){
			if (res.errcode !== 0) {
				toaster.error({ title: "/api/as/sc/dict/getSaleCategoryList", body: res.errmsg });
				return false;
			}

			var flag = false;
			for (var index = 0; index < res.data.length; index++) {
				var element = res.data[index];
				if (element.value == info.sale_category) {
					flag = true;
				}
			}
			if (!flag) {
				toaster.error({ title: "您的账号无权查看该类型销售品", body: "请配置销售品权限" });
				return false;
			}

			$modal.open({
				template: require('../views/productInfo1.html'),
				controller: 'infoProduct1',
				url: '/infoProduct/:id',
				size: 'lg',
				resolve: {
					'productid': function () {
						return info.id;
					},
					date2str: function (utilservice) {
						return utilservice.getDate;
					},
					str2date: function (utilservice) {
						return utilservice.str2date;
					},
					auditing: function () {
						return false;
					}
				}
			});
		});
	}


};